package com.lishe.lishe.lishe.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lishe.lishe.R;

/**
 * Created by shino on 2017/12/25.
 */

public class MListDialog extends Dialog {


    public MListDialog(@NonNull Context context) {
        super(context);
    }

    public MListDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected MListDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        BaseAdapter adapter;
        private Context context;
        private MListDialog dialog;

        public Builder(Context context) {
            this.context = context;
        }

        public void setAdapter(BaseAdapter adapter) {
            this.adapter = adapter;
            if (listView != null) {
                listView.setAdapter(adapter);
            }
        }


        /**
         * 创建popupwindow
         */
        public MListDialog create() {
            dialog = new MListDialog(context, R.style.MListPopWindow);
            View view = initView(dialog);
            dialog.setContentView(view);
            Window dialogWindow = dialog.getWindow();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
            dialogWindow.setAttributes(lp);
            return dialog;
        }

        ListView listView;

        private View initView(final MListDialog popupwindow) {
            View view = LayoutInflater.from(context).inflate(R.layout.popwindow_company_list, null);
            listView = view.findViewById(R.id.listview);
            if (adapter != null) {
                listView.setAdapter(adapter);
            }
            TextView cancle = view.findViewById(R.id.cancle);
            cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupwindow.dismiss();
                }
            });
            return view;
        }

        public ListView getListView() {
            return listView;
        }

        //关掉
        public void dissmiss() {
            dialog.dismiss();
        }
    }
}
